package com.tianqu.tis.core.webservice;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.entity.TmsDetectNormal;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.entity.TmsDetectSenior;
import com.tianqu.tms.entity.User;

public class XmlResolveController {
	private static Logger logger = Logger.getLogger(XmlResolveController.class);//log
	/**
	 * 车辆信息
	 * 
	 * @return
	 */
	public static byte[] carInfo() { 

		try {
			TxController controller =TxController.getTxController();
			controller.begin();
			TmsDetectInfo car = TmsDetectInfo.getInstance();
			List<TmsDetectInfo> list = car.findAllWDIpadCarInfoByStatus(2);
			if(list.size()<1){
				controller.commit();
				return null;  
			}
			File file = new File("BaseInfo.xml");
			Element base = new Element(TisXmlConstants.BASEINFO);
			// 根节点添加到文档中；
			Document Doc = new Document(base);
			Codes code = Codes.getInstance();
			for (int i = 0; i < list.size(); i++) {
				TmsDetectInfo tisCar = list.get(i);
				if(tisCar==null){
					break;//这块不应该是continue 为空的话应该直接跳出循环重新进入
				}
				// 创建根节点 list;
				Element root = new Element(TisXmlConstants.CARS);
				root.addContent(new Element(TisXmlConstants.ID).setText(tisCar.getCarSerial()));
				root.addContent(new Element(TisXmlConstants.NAME).setText(tisCar.getCarName()));
				root.addContent(new Element(TisXmlConstants.PLATENUM).setText(tisCar.getCarLicense()));
				root.addContent(new Element(TisXmlConstants.SPEEDVARIATOR).setText(String.valueOf(tisCar.getGearbox())));
				root.addContent(new Element(TisXmlConstants.COLOR).setText(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, Integer.parseInt(tisCar.getColor()))));
				root.addContent(new Element(TisXmlConstants.REGISTERDATE).setText(String.valueOf(tisCar.getRegDate())));
				root.addContent(new Element(TisXmlConstants.CHECKDATE).setText(String.valueOf(tisCar.getCarCheckInsur())));
				root.addContent(new Element(TisXmlConstants.INSURANCEDATE).setText(String.valueOf(tisCar.getTrafficInsur())));
				root.addContent(new Element(TisXmlConstants.FUELDICT).setText(String.valueOf(tisCar.getFuelDict())));
				root.addContent(new Element(TisXmlConstants.USEPROPERTY).setText(String.valueOf(tisCar.getNature())));
				root.addContent(new Element(TisXmlConstants.EXHAUST).setText(String.valueOf(tisCar.getExhaust())));
				base.addContent(root);
				XMLOutputter XMLOut = new XMLOutputter(format());
				XMLOut.output(Doc, new FileOutputStream(file));
			
				//更新车辆状态
				car.updateCarStatus(tisCar.getCarSerial(), 4);
			}
			InputStream in = new FileInputStream(file);
			byte bytes[] = new byte[in.available()];
			// 从输入流in中,将 bytes.length 个字节的数据读入字节数组bytes中
			in.read(bytes);
			in.close();
			controller.commit();
			return bytes;
		} catch (IOException e) {
			logger.error("出现异常"+e.getMessage());
			return null;
		} catch (TmsDaoException e) {
			logger.error("出现异常"+e.getMessage());
			e.printStackTrace();
			return null;
		} catch (TransactionException e) {
			logger.error("出现异常"+e.getMessage());
			return null;
			
		}

	}

	/**
	 * 用户信息
	 * 
	 * @return
	 */
	public static byte[] user() {
		try {			
			TxController controller =TxController.getTxController();
			controller.begin();
			User user = User.getInstance();
			List<User> list = user.findIpadUser();
			if(list.size()<1){
				controller.commit();
				return null;
			}
			File file = new File("User.xml");
			Element base = new Element(TisXmlConstants.BASEUSER);
			// 根节点添加到文档中；
			Document Doc = new Document(base);
			for (int i = 0; i < list.size(); i++) {
				User use = list.get(i);
				// 创建根节点 list;
				Element root = new Element(TisXmlConstants.USERS);
				root.addContent(new Element(TisXmlConstants.USERID).setText(use.getLoginId()));
				root.addContent(new Element(TisXmlConstants.PASSWORD).setText(use.getPassword()));
				root.addContent(new Element(TisXmlConstants.USERNAME).setText(use.getRealName()));
				root.addContent(new Element(TisXmlConstants.USERTYPE).setText(use.getUserType()));
				base.addContent(root);
				XMLOutputter XMLOut = new XMLOutputter(format());
				XMLOut.output(Doc, new FileOutputStream(file));
			}
			InputStream in = new FileInputStream(file);
			byte bytes[] = new byte[in.available()];
			// 从输入流in中,将 bytes.length 个字节的数据读入字节数组bytes中
			in.read(bytes);
			in.close();
			controller.commit();
			return bytes;
		} catch (IOException e) {
			logger.error("出现异常"+e.getMessage());
		} catch (TmsDaoException e) {
			logger.error("出现异常"+e.getMessage());
		} catch (TransactionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 检测信息
	 * 
	 * @throws FileNotFoundException
	 */
	public static boolean CarDetects(String CarId, int Type, byte[] Normal) {
		logger.debug("CarDetects进入检测信息回传类..............................");
		//logger.debug("接收到的参数：   车辆ID："+CarId + "检测模式：" +Type + "检测信息byte64流：" +Arrays.toString(Normal));
		try {

			if (!StringUtil.isNullOrBlank(CarId)&& !StringUtil.isNullOrBlank(String.valueOf(Type))) {
				TxController controller =TxController.getTxController();
				controller.begin();
                TmsDetectInfo tisCar = TmsDetectInfo.getInstance();
                tisCar = tisCar.getWDIpadCarInfoBySerial(CarId,1);
                //判断车辆是否存在 或车辆状态是否等于4
                if(tisCar==null||tisCar.getStatus()!=4){
                	logger.error("服务器车辆状态以改变!");
                	controller.commit();
                	return false;
                	 
                }
				byte[] bytes = Normal;
				if (bytes.length == 0) {
					logger.error("上传文件内容为空!");
					controller.commit();
					return false;
					
				}
				InputStream in = new ByteArrayInputStream(bytes);
				in.close();
				
				SAXBuilder builder = new SAXBuilder();
				Document document = builder.build(in);// 获得文档对象
				Element root = document.getRootElement();// 获得根节点
				
				//Element base = new Element("CarDetect");
				// 基本检测
				if (Type == 1) {
					logger.debug("录入普通检测..................................");
					TmsDetectNormal Detenormal = new TmsDetectNormal();
					Detenormal.setId(0);
					Detenormal.setDisasterCars(StringUtil.getNone(root.getChildText(TisXmlConstants.DISASTERCARS)));
					Detenormal.setFrontLeftChk1(StringUtil.getNone(root.getChildText("Frontleftchk1")));
					Detenormal.setFrontLeftChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK2)));
					Detenormal.setFrontLeftChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK3)));
					Detenormal.setFrontLeftChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK4)));
					Detenormal.setFrontLeftChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK5)));
					Detenormal.setFrontLeftChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK6)));
					Detenormal.setFrontLeftChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK7)));
					Detenormal.setFrontLeftChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK8)));
					Detenormal.setFrontLeftChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTLEFTCHK9)));
					Detenormal.setInsideChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK1)));
					Detenormal.setInsideChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK2)));
					Detenormal.setInsideChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK3)));
					Detenormal.setInsideChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK4)));
					Detenormal.setInsideChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK5)));
					Detenormal.setInsideChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK6)));
					Detenormal.setInsideChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK7)));
					Detenormal.setInsideChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK8)));
					Detenormal.setInsideChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK9)));
					Detenormal.setInsideChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK10)));
					Detenormal.setInsideChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK11)));
					Detenormal.setInsideChk12(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK12)));
					Detenormal.setInsideChk13(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK13)));
					Detenormal.setInsideChk14(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK14)));
					Detenormal.setInsideChk15(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK15)));
					Detenormal.setInsideChk16(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK16)));
					Detenormal.setInsideChk17(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK17)));
					Detenormal.setInsideChk18(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK18)));
					Detenormal.setInsideChk19(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK19)));
					Detenormal.setInsideChk20(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK20)));
					Detenormal.setInsideChk21(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK21)));
					Detenormal.setInsideChk22(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK22)));
					Detenormal.setInsideChk23(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK23)));
					Detenormal.setInsideChk24(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK24)));
					Detenormal.setInsideChk25(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK25)));
					Detenormal.setInsideChk26(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK26)));
					Detenormal.setInsideChk27(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK27)));
					Detenormal.setInsideChk28(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK28)));
					Detenormal.setInsideChk29(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK29)));
					Detenormal.setInsideChk30(StringUtil.getNone(root.getChildText(TisXmlConstants.INSIDECHK30)));
					Detenormal.setPanelFenerLhChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK1)));
					Detenormal.setPanelFenerLhChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK2)));
					Detenormal.setPanelFenerLhChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK3)));
					Detenormal.setPanelFenerLhChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK4)));
					Detenormal.setPanelFenerLhChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK5)));
					Detenormal.setPanelFenerLhChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERLHCHK6)));
					Detenormal.setDeadAheadChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK1)));
					Detenormal.setDeadAheadChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK2)));
					Detenormal.setDeadAheadChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK3)));
					Detenormal.setDeadAheadChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK4)));
					Detenormal.setDeadAheadChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK5)));
					Detenormal.setDeadAheadChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.DEADAHEADCHK6)));
					Detenormal.setEngineChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK1)));
					Detenormal.setEngineChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK2)));
					Detenormal.setEngineChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK3)));
					Detenormal.setEngineChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK4)));
					Detenormal.setEngineChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK5)));
					Detenormal.setEngineChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK6)));
					Detenormal.setEngineChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK7)));
					Detenormal.setEngineChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.ENGINECHK8)));
					Detenormal.setPanelFenerRhChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK1)));
					Detenormal.setPanelFenerRhChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK2)));
					Detenormal.setPanelFenerRhChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK3)));
					Detenormal.setPanelFenerRhChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK4)));
					Detenormal.setPanelFenerRhChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK5)));
					Detenormal.setPanelFenerRhChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.PANELFENERRHCHK6)));
					Detenormal.setFrontRightChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK1)));
					Detenormal.setFrontRightChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK2)));
					Detenormal.setFrontRightChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK3)));
					Detenormal.setFrontRightChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK4)));
					Detenormal.setFrontRightChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK5)));
					Detenormal.setFrontRightChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK6)));
					Detenormal.setFrontRightChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK7)));
					Detenormal.setFrontRightChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK8)));
					Detenormal.setFrontRightChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK9)));
					Detenormal.setFrontRightChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK10)));
					Detenormal.setFrontRightChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK11)));
					Detenormal.setFrontRightChk12(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK12)));
					Detenormal.setFrontRightChk13(StringUtil.getNone(root.getChildText(TisXmlConstants.FRONTRIGHTCHK13)));
					Detenormal.setRearRightChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK1)));
					Detenormal.setRearRightChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK2)));
					Detenormal.setRearRightChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK3)));
					Detenormal.setRearRightChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK4)));
					Detenormal.setRearRightChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK5)));
					Detenormal.setRearRightChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK6)));
					Detenormal.setRearRightChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK7)));
					Detenormal.setRearRightChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK8)));
					Detenormal.setRearRightChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK9)));
					Detenormal.setRearRightChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK10)));
					Detenormal.setRearRightChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.REARRIGHTCHK11)));
					Detenormal.setFenderRaChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK1)));
					Detenormal.setFenderRaChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK2)));
					Detenormal.setFenderRaChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK3)));
					Detenormal.setFenderRaChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK4)));
					Detenormal.setFenderRaChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK5)));
					Detenormal.setFenderRaChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERRACHK6)));
					Detenormal.setRightAsternChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK1)));
					Detenormal.setRightAsternChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK2)));
					Detenormal.setRightAsternChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK3)));
					Detenormal.setRightAsternChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK4)));
					Detenormal.setRightAsternChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK5)));
					Detenormal.setRightAsternChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.RIGHTASTERNCHK6)));
					Detenormal.setLuggageChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK1)));
					Detenormal.setLuggageChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK2)));
					Detenormal.setLuggageChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK3)));
					Detenormal.setLuggageChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK4)));
					Detenormal.setLuggageChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK5)));
					Detenormal.setLuggageChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK6)));
					Detenormal.setLuggageChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK7)));
					Detenormal.setLuggageChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK8)));
					Detenormal.setLuggageChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK9)));
					Detenormal.setLuggageChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.LUGGAGECHK10)));
					Detenormal.setFenderLaChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK1)));
					Detenormal.setFenderLaChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK2)));
					Detenormal.setFenderLaChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK3)));
					Detenormal.setFenderLaChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK4)));
					Detenormal.setFenderLaChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK5)));
					Detenormal.setFenderLaChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.FENDERLACHK6)));
					Detenormal.setRearLeftChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK1)));
					Detenormal.setRearLeftChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK2)));
					Detenormal.setRearLeftChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK3)));
					Detenormal.setRearLeftChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK4)));
					Detenormal.setRearLeftChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK5)));
					Detenormal.setRearLeftChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK6)));
					Detenormal.setRearLeftChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK7)));
					Detenormal.setRearLeftChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK8)));
					Detenormal.setRearLeftChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK9)));
					Detenormal.setRearLeftChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK10)));
					Detenormal.setRearLeftChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.REARLEFTCHK11)));
					Detenormal.setCarRoofChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.CARROOFCHK1)));
					Detenormal.setExternalGrade(StringUtil.getDouble(root.getChildText(TisXmlConstants.EXTERNALGRADE)));
					Detenormal.setInternalGrade(StringUtil.getDouble(root.getChildText(TisXmlConstants.INTERNALGRADE)));
					Detenormal.setEngineGrade(StringUtil.getDouble(root.getChildText(TisXmlConstants.ENGINEGRADE)));
					Detenormal.setElectrinicsGrade(StringUtil.getDouble(root.getChildText(TisXmlConstants.ELECTRINICSGRADE)));
					Detenormal.setRestsGrade(StringUtil.getDouble(root.getChildText(TisXmlConstants.RESTSGRADE)));
					TmsDetectNormal normal = TmsDetectNormal.getInstance();
					int carDetenormalId = normal.AddCarNormalDetectInfo(Detenormal);
				
					logger.debug("普通检测存入返回的ID："+carDetenormalId);
					//解析车辆基本信息表信息
					tisCar.setSkylight(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.SKYLIGHT)));
					tisCar.setLeatherSeats(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.LEATHERSEATS)));
					tisCar.setAluminumRim(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.ALUMINUMRIM)));
					tisCar.setGpsNavigation(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.GPSNAVIGATION)));
					tisCar.setAutoWindow(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.AUTOWINDOW)));
					tisCar.setAbs(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.ABS)));
					tisCar.setSrsNumber(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.STSNUMBER)));
					tisCar.setFourWheelDrive(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.FOURWHEELDRIVE)));
					tisCar.setTotalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.TOTALSCORE)));
					tisCar.setExternalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.EXTERNALSCORE)));
					tisCar.setInternalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.INTERNALSCORE)));
					tisCar.setTroubleCar(StringUtil.getNoneber(root.getChildText(TisXmlConstants.TROUBLECAR)));
					tisCar.setCarDescribe(StringUtil.getNone(root.getChildText(TisXmlConstants.CARDESCRIBE)));
					tisCar.setMileage(Integer.parseInt(StringUtil.getNone(root.getChildText(TisXmlConstants.MILEAGE))));//表显里程
					//tisCar.setchkTime(StringUtil.getNoneber(root.getChildText(TisXmlConstants.CHKTIME)));
					//车辆状态
					tisCar.setCarSerial(CarId);
					tisCar.setCarDetectId(carDetenormalId);
					tisCar.setChkModel(Type);
					tisCar.setStatus(5);
					//修改车辆信息 ,评分等级  检测ID 检测模式
					tisCar.updateCarInfoByNormalDetectInfo(tisCar,1);
					logger.debug("普通检测录入结束..................................");

					// 高级检测
				} else if (Type == 2) {
					logger.debug("录入高级检测..................................");
					TmsDetectSenior Detesenio = new TmsDetectSenior();
					Detesenio.setId(0);
					Detesenio.setBodyChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK1)));
					Detesenio.setBodyChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK2)));
					Detesenio.setBodyChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK3)));
					Detesenio.setBodyChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK4)));
					Detesenio.setBodyChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK5)));
					Detesenio.setBodyChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK6)));
					Detesenio.setBodyChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK7)));
					Detesenio.setBodyChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK8)));
					Detesenio.setBodyChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK9)));
					Detesenio.setBodyChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK10)));
					Detesenio.setBodyChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK11)));
					Detesenio.setBodyChk12(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK12)));
					Detesenio.setBodyChk13(StringUtil.getNone(root.getChildText(TisXmlConstants.BODYCHK13)));
					Detesenio.setOutwardChk1(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK1)));
					Detesenio.setOutwardChk2(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK2)));
					Detesenio.setOutwardChk3(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK3)));
					Detesenio.setOutwardChk4(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK4)));
					Detesenio.setOutwardChk5(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK5)));
					Detesenio.setOutwardChk6(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK6)));
					Detesenio.setOutwardChk7(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK7)));
					Detesenio.setOutwardChk8(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK8)));
					Detesenio.setOutwardChk9(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK9)));
					Detesenio.setOutwardChk10(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK10)));
					Detesenio.setOutwardChk11(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK11)));
					Detesenio.setOutwardChk12(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK12)));
					Detesenio.setOutwardChk13(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK13)));
					Detesenio.setOutwardChk14(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK14)));
					Detesenio.setOutwardChk15(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK15)));
					Detesenio.setOutwardChk16(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK16)));
					Detesenio.setOutwardChk17(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK17)));
					Detesenio.setOutwardChk18(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK18)));
					Detesenio.setOutwardChk19(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK19)));
					Detesenio.setOutwardChk20(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK20)));
					Detesenio.setOutwardChk21(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK21)));
					Detesenio.setOutwardChk22(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK22)));
					Detesenio.setOutwardChk23(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK23)));
					Detesenio.setOutwardChk25(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK25)));
					Detesenio.setOutwardChk26(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK26)));
					Detesenio.setOutwardChk27(StringUtil.getNone(root.getChildText(TisXmlConstants.OUTWARDCHK27)));
					Detesenio.setEngineChk1(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK1)));
					Detesenio.setEngineChk2(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK2)));
					Detesenio.setEngineChk3(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK3)));
					Detesenio.setEngineChk4(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK4)));
					Detesenio.setEngineChk5(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK5)));
					Detesenio.setEngineChk6(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK6)));
					Detesenio.setEngineChk7(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK7)));
					Detesenio.setEngineChk8(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK8)));
					Detesenio.setEngineChk9(StringUtil.getNoneber(root.getChildText(TisXmlConstants.ENGINECHK9)));
					Detesenio.setPilotChk1(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK1)));
					Detesenio.setPilotChk2(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK2)));
					Detesenio.setPilotChk3(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK3)));
					Detesenio.setPilotChk4(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK4)));
					Detesenio.setPilotChk5(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK5)));
					Detesenio.setPilotChk6(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK6)));
					Detesenio.setPilotChk7(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK7)));
					Detesenio.setPilotChk8(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK8)));
					Detesenio.setPilotChk9(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK9)));
					Detesenio.setPilotChk10(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK10)));
					Detesenio.setPilotChk11(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK11)));
					Detesenio.setPilotChk12(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK12)));
					Detesenio.setPilotChk13(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK13)));
					Detesenio.setPilotChk14(StringUtil.getNoneber(root.getChildText(TisXmlConstants.PILOTCHK14)));
					Detesenio.setStartChk1(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK1)));
					Detesenio.setStartChk2(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK2)));
					Detesenio.setStartChk3(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK3)));
					Detesenio.setStartChk4(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK4)));
					Detesenio.setStartChk5(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK5)));
					Detesenio.setStartChk6(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK6)));
					Detesenio.setStartChk7(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK7)));
					Detesenio.setStartChk8(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK8)));
					Detesenio.setStartChk9(StringUtil.getNoneber(root.getChildText(TisXmlConstants.STARTCHK9)));
					Detesenio.setRunChk1(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK1)));
					Detesenio.setRunChk2(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK2)));
					Detesenio.setRunChk3(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK3)));
					Detesenio.setRunChk4(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK4)));
					Detesenio.setRunChk5(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK5)));
					Detesenio.setRunChk6(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK6)));
					Detesenio.setRunChk7(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK7)));
					Detesenio.setRunChk8(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK8)));
					Detesenio.setRunChk9(StringUtil.getNoneber(root.getChildText(TisXmlConstants.RUNCHK9)));
					Detesenio.setBottomChk1(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK1)));
					Detesenio.setBottomChk2(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK2)));
					Detesenio.setBottomChk3(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK3)));
					Detesenio.setBottomChk4(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK4)));
					Detesenio.setBottomChk5(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK5)));
					Detesenio.setBottomChk6(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK6)));
					Detesenio.setBottomChk7(StringUtil.getNoneber(root.getChildText(TisXmlConstants.BOTTOMCHK7)));
					Detesenio.setOutwardScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.OUTWARDSCORE)));
					Detesenio.setEngineScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.ENGINESCORE)));
					Detesenio.setPilotScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.PILOTSCORE)));
					Detesenio.setStartScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.STARTSCORE)));
					Detesenio.setRunScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.RUNSCORE)));
					Detesenio.setBottomScore(StringUtil.getDouble(root.getChildText(TisXmlConstants.BOTTOMSCORE)));
					TmsDetectSenior senior = TmsDetectSenior.getInstance();
					int carDetesenioId = senior.insertCarSeniorDetectInfo(Detesenio);
					logger.debug("高级检测存入返回的ID："+carDetesenioId);
					//解析车辆基本信息表信息
					tisCar.setSkylight(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.SKYLIGHT)));
					tisCar.setLeatherSeats(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.LEATHERSEATS)));
					tisCar.setAluminumRim(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.ALUMINUMRIM)));
					tisCar.setGpsNavigation(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.GPSNAVIGATION)));
					tisCar.setAutoWindow(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.AUTOWINDOW)));
					tisCar.setAbs(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.ABS)));
					tisCar.setSrsNumber(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.STSNUMBER)));
					tisCar.setFourWheelDrive(StringUtil.getNoneber2(root.getChildText(TisXmlConstants.FOURWHEELDRIVE)));
					tisCar.setTotalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.TOTALSCORE)));
					tisCar.setExternalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.EXTERNALSCORE)));
					tisCar.setInternalScore(StringUtil.getNone(root.getChildText(TisXmlConstants.INTERNALSCORE)));
					tisCar.setTroubleCar(StringUtil.getNoneber(root.getChildText(TisXmlConstants.TROUBLECAR)));
					tisCar.setCarDescribe(StringUtil.getNone(root.getChildText(TisXmlConstants.CARDESCRIBE)));
					tisCar.setMileage(Integer.parseInt(StringUtil.getNone(root.getChildText(TisXmlConstants.MILEAGE))));//表显里程
					//tisCar.setchkTime(StringUtil.getNoneber(root.getChildText(TisXmlConstants.CHKTIME)));
					//车辆状态
					tisCar.setCarSerial(CarId);
					tisCar.setChkModel(Type);
					tisCar.setStatus(5);					
					tisCar.setCarDetectId(carDetesenioId);
					//修改车辆信息 ,评分等级  检测ID 检测模式
                     TmsDetectInfo tisCarInfo = TmsDetectInfo.getInstance();
                     tisCarInfo.updateCarInfoByNormalDetectInfo(tisCar,2);
                     logger.debug("高级检测录入结束..................................");
					// 参数有误
				} else {
					logger.error("检测模式传入有误!");
					controller.commit();
					return false;
				}
				controller.commit();
                logger.debug("CarDetects离开检测信息回传类..............................");
				return true;
			}
		} catch (Exception e) {
			logger.error("出现异常"+e.getMessage());
			e.printStackTrace();
			return false;
		}
		return false;
	}

	/***
	 * 图片信息
	 * 车辆ID,图片类型,图片对应KEY,图片文件
	 * @throws IOException
	 * @throws JDOMException
	 */
	public static boolean CarImages(String CarId,int ImageType , int ImageKey,byte[] Image) {
		logger.debug("CarImages图片上传start........................");
		//logger.error("接收到的参数             车辆ID:"+CarId+"图片类型ImageType:"+ImageType+"图片位置ImageKey:"+ImageKey + "图片信息byte64流：" +Arrays.toString(Image));
		TxController controller =TxController.getTxController();
		String FileName="";
		try {
			if (!StringUtil.isNullOrBlank(CarId) && ImageType > 0 && ImageKey > 0&& Image.length > 0) {
				controller.begin();
				//1.查询车辆信息是否存在，且车辆状态是否符合上传检测图片内容
				  TmsDetectInfo tisCar = TmsDetectInfo.getInstance();
	              tisCar = tisCar.getWDIpadCarInfoBySerial(CarId,1);
	                //判断车辆是否存在 或车辆状态是否等于5
	              logger.error("车辆检测模式:"+tisCar.getChkModel());
	                if(tisCar==null||tisCar.getStatus()!=5||tisCar.getChkModel()<1){
	                	logger.error("车辆状态以改变或者无检测信息");
	                	controller.commit();
	                	return false;
	                }
	                
	                FileName=Registry.getConfig(TmsConstants.UPLOAD_CONTEXT_ROOT_KEY)+"/"+CarId+"/"+CarId+tisCar.getCarDetectId()+tisCar.getChkModel()+ImageType+ImageKey+".jpg";
	              //2.查询图片是否重复，如果重复 覆盖服务器上车辆图片内容 条件:复合主键 检测ID+检测类型+图片类型+图片KEY值
	                TmsDetectPic tisPics = TmsDetectPic.getInstance();
	                tisPics.setCarDetectId(tisCar.getCarDetectId());
	                tisPics.setChkModel(tisCar.getChkModel());
	                tisPics.setImgType(ImageType);
	                tisPics.setImgKey(String.valueOf(ImageKey));
	                tisPics=tisPics.getTisPic(tisPics);
	                //3.图片存在更新数据库
	                if(tisPics==null){
		                //插入数据库信息
		                TmsDetectPic tisPic =TmsDetectPic.getInstance();
		                tisPic.setCarDetectId(tisCar.getCarDetectId());
		                tisPic.setChkModel(tisCar.getChkModel());
		                tisPic.setImgType(ImageType);
		                tisPic.setImgKey(String.valueOf(ImageKey));
		               	tisPic.setImgPath(FileName); 
		                tisPic.insertTisPic(tisPic);	
	                }
	                FileName="/"+CarId+tisCar.getCarDetectId()+tisCar.getChkModel()+ImageType+ImageKey+".jpg";
	        		//图片名称规则 ： 车辆ID+检测模式+图片类型+图片对应的key值+".jpg"
	                String dir =  ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_CONTEXT_ROOT_KEY)+"/"+CarId+"/");
					byte[] bytes = Image;
					if (bytes.length == 0) {
						logger.error("文件为空!");
						controller.commit();
						return false;
					}
					
					FileOutputStream fileOutPutStream = new FileOutputStream(dir+FileName);
			        //将字节数组bytes中的数据，全部写入输出流fileOutPutStream中
					//ByteBuffer buffer = ByteBuffer.wrap(getMsgHead(msgBytes.length));  
			        fileOutPutStream.write(bytes);
			        fileOutPutStream.flush();
			        fileOutPutStream.close();
			        fileOutPutStream.getChannel().close();
				//插入图片信息
				controller.commit();
				logger.debug("CarImages图片上传end 在不出现异常的情况下.........................");
			} else {
				logger.error("传入参数有误!");
				controller.commit();
				return false;
			}
		} catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			e.printStackTrace();
		}
//		logger.debug("CarImages图片上传end看看是不是走完了........................");
		return true;
	}

	
	/***
	 * 格式化方法
	 * 
	 * @return
	 */
	private static Format format() {
		Format format = Format.getCompactFormat();
		format.setEncoding("utf-8");
		format.setIndent(" ");
		return format;
	}


}
